<?php

namespace magein\think\utils\middleware;

use magein\think\utils\api\ApiResponse;
use magein\think\utils\constants\Code;
use magein\think\utils\service\Auth;
use think\Request;
use think\Response;

class AuthorizationMiddleware
{
    /**
     * 处理请求
     *
     * @param Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        if (!Auth::authorization()) {
            return ApiResponse::error('非法请求', Code::AUTHORIZATION_EMPTY);
        }

        if (!Auth::check()) {
            return ApiResponse::error('非法请求', Code::AUTHORIZATION_ILLEGAL);
        }

        return $next($request);
    }
}
